package com.rupertjones.globalcron.web.service;

import com.rupertjones.globalcron.domain.AuditLog;
import com.rupertjones.globalcron.domain.User;
import com.rupertjones.globalcron.domain.dao.AuditLogDAO;
import com.rupertjones.globalcron.domain.dao.UserDAO;
import com.rupertjones.globalcron.web.model.DisplayableAuditLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>&copy Rupert Jones 2012</p>
 *
 * @author rup
 */
@Service
public class DefaultAuditLogService implements AuditLogService {

    @Autowired
    private AuditLogDAO auditLogDAO;

    @Autowired
    private UserDAO userDAO;

    @Override
    public List<DisplayableAuditLog> findAll() {
        return toDisplay(auditLogDAO.findAllNotUser(userDAO.findByUsername("system")));
    }

    public List<DisplayableAuditLog> findAll(User user) {
        return toDisplay(auditLogDAO.findAllByUser(user));
    }

    private List<DisplayableAuditLog> toDisplay(List<AuditLog> logs) {
        List<DisplayableAuditLog> displayableAuditLogs = new ArrayList<DisplayableAuditLog>();
        for(AuditLog thisLog : logs) {
            displayableAuditLogs.add(toDisplay(thisLog));
        }
        return displayableAuditLogs;
    }

    private DisplayableAuditLog toDisplay(AuditLog log) {
        return new DisplayableAuditLog(log);
    }
}
